package com.study.shirojwtredis.shiro.config;


import com.study.shirojwtredis.shiro.filter.ShiroFilterChainManager;
import com.study.shirojwtredis.shiro.filter.StatelessWebSubjectFactory;
import com.study.shirojwtredis.shiro.matcher.CustomCredentialsMatcher;
import com.study.shirojwtredis.shiro.matcher.JwtMatcher;
import com.study.shirojwtredis.shiro.realm.AonModularRealmAuthenticator;
import com.study.shirojwtredis.shiro.realm.JwtRealm;
import com.study.shirojwtredis.shiro.realm.PasswordRealm;
import com.study.shirojwtredis.shiro.realm.RealmManager;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.Authorizer;
import org.apache.shiro.authz.ModularRealmAuthorizer;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/*********************************************************
 * @Author ONE_LLX
 * @Description //TODO 
 * @Date 18:12 2019/11/16
 * @Class ShiroConfig
 * @Version v1.0
 *********************************************************/
@Configuration
public class ShiroConfig {


    /**
     * 验证方式
     * @return
     */

    @Bean
    public DefaultWebSecurityManager securityManager(RealmManager realmManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setAuthenticator(new AonModularRealmAuthenticator());
        securityManager.setRealms(realmManager.initGetRealm());
        // 无状态subjectFactory设置
        DefaultSessionStorageEvaluator evaluator = (DefaultSessionStorageEvaluator)((DefaultSubjectDAO) securityManager.getSubjectDAO()).getSessionStorageEvaluator();
        evaluator.setSessionStorageEnabled(Boolean.FALSE);
        StatelessWebSubjectFactory subjectFactory = new StatelessWebSubjectFactory();
        securityManager.setSubjectFactory(subjectFactory);

        SecurityUtils.setSecurityManager(securityManager);
        return securityManager;
    }
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager,ShiroFilterChainManager filterChainManager) {
        RestShiroFilterFactoryBean shiroFilterFactoryBean = new RestShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        shiroFilterFactoryBean.setFilters(filterChainManager.initGetFilters());
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainManager.initGetFilterChain());
        return shiroFilterFactoryBean;
    }
}
